<template>
	<view class="margin-top bg-white padding-lr">
		<u-form :model="info" ref="form" label-width="auto">
			<u-form-item label="角色代码" prop="roleCode" required><u-input v-model="info.roleCode" /></u-form-item>
			<u-form-item label="角色名称" prop="roleName" required><u-input v-model="info.roleName" /></u-form-item>
			<u-form-item label="角色别名" prop="aliasName"><u-input v-model="info.aliasName" /></u-form-item>
			<u-form-item label="附加信息" prop="addInfo"><u-input v-model="info.addInfo" /></u-form-item>
			<u-form-item label="描述信息" prop="roleDesc"><u-input v-model="info.roleDesc" /></u-form-item>
			<u-form-item label="序号" prop="sequence"><u-input v-model="info.sequence" type="number" /></u-form-item>
			<view class="padding-tb"><u-button type="primary" @click="save">保存</u-button></view>
		</u-form>
	</view>
</template>

<script>
export default {
	onLoad: function(option) {
		if (option.info) {
			this.info = JSON.parse(decodeURIComponent(option.info));
		}
	},
	onReady: function() {
		this.$refs.form.setRules(this.rules);
	},
	data() {
		return {
			info: {
				id: '',
				roleCode: '',
				roleName: '',
				sequence: "0" // 序号
			},
			rules: {
				roleCode: [{ required: true, message: '必填', trigger: ['change', 'blur'] }],
				roleName: [{ required: true, message: '必填', trigger: ['change', 'blur'] }]
			}
		};
	},
	watch: {
		'info.roleCode': function() {
			this.info.roleCode = (this.info.roleCode || '').toUpperCase();
		}
	},
	methods: {
		// 保存用户
		save: function() {
			this.$refs['form'].validate(valid => {
				if (valid) {
					this.$request.post(
						'/admin/sys/role/save',
						this.info,
						res => {
							this.$utils.showModal('保存成功！', () => {
								uni.navigateBack({
									delta: 1
								});
							});
						},
						true,
						true,
						true
					);
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
